// pages/bindbank/bindbank.js
const app = getApp();
var images = [];
var up ='';
var center = '';
var down = '';
Page({

  /**
   * 页面的初始数据11
   */
  data: {
    up:'/image/idnumber1.png',
    center:'/image/idnumber2.png',
    down:'/image/idnumber3.png',
    username:'',
    idnumber:'',
    password:'',
    repassword:'',
    idnumberup:'',
    idnumberdow:'',
    idnumberuser:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this
    wx.setNavigationBarTitle({
      title: '实名认证',
    });
    wx.request({
      url: app.serverUrl + 'findBank',
      method: 'post',
      data: {
        openid: app.openid,
        // openid:'ojVgQ5dFzmM8mdljTwt_-9OCY0Kc'
      },
      success: function (res) {
        console.log(res);
        if (res.data.flag == 'success') {
          self.setData({
            up: res.data.data.idnumberup,
            center: res.data.data.idnumberdow,
            down: res.data.data.idnumberuser,
            username: res.data.data.username,
            idnumber: res.data.data.idnumber,
            password: res.data.data.password,
            repassword: res.data.data.password,
            idnumberup: res.data.data.idnumberup,
            idnumberdow: res.data.data.idnumberdow,
            idnumberuser: res.data.data.idnumberuser,
          });
        }
      }
    })

  },
  addImag(e) {
    var self = this;
    var thisid = e.target.id;
    // var images =[];
    wx.chooseImage({
      count: 3, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        // wx.showLoading({
        //   title: '正在上传图片',
        // })
        var tempFilePaths = res.tempFilePaths
        // self.setData({
        //   images: tempFilePaths
        // })
        wx.showLoading({
          title: '正在上传...',
        })
        // for (var i = 0; i < tempFilePaths.length; i++) {
          wx.uploadFile({
            url: app.serverUrl + 'articleUp', //仅为示例，非真实的接口地址
            filePath: tempFilePaths[0],
            name: 'articleimg',
            success: function (res) {
              wx.hideLoading({
                title: '',
              })
              var data = JSON.parse(res.data);
              if (thisid == 'up'){
                up=data.data;
                self.setData({
                  up: data.data,
                  idnumberup:data.data
                })
              }else if(thisid == 'center'){
                center=data.data;
                self.setData({
                  center: data.data,
                  idnumberdow:data.data
                })
              } else if (thisid == 'down'){
                down=data.data;
                self.setData({
                  down: data.data,
                  idnumberuser: data.data
                })
              }
              
              // images.push();
              images[0]=up;
              images[1]=center;
              images[2]=down;
              // linImgs.push(data.data);
              // if (linImgs.length == self.data.imags.length) {
              //   wx.hideLoading()
              // }
            },
            fail: function (res) {
              // console.log(res);

            }
          })
        // }
      }
    })

  },
  addbank:function(){
    // wx.showLoading({
    //   title: 'tijia',
    // })
    
    var self = this;
    if(self.data.username==''||self.data.username==null){
      wx.showToast({
        title: '请输入姓名',
        icon:'none'
      })
    }else if(self.data.idnumber==''||self.data.idnumber==null){
      wx.showToast({
        title: '请输入身份证号',
        icon: 'none'
      })
    }else if(self.data.idnumber.length<18){
      wx.showToast({
        title: '身份证格式有误',
        icon: 'none'
      })
    }else if(self.data.password==''||self.data.password==null){
      wx.showToast({
        title: '请输入提现密码',
        icon: 'none'
      })
    } else if (self.data.password != self.data.repassword){
      wx.showToast({
        title: '两次密码不一致',
        icon: 'none'
      })
    } else if (self.data.idnumberup == '' || self.data.idnumberup == null || self.data.idnumberdow == '' || self.data.idnumberdow == null || self.data.idnumberuser == '' || self.data.idnumberuser == null){
      wx.showToast({
        title: '请选择身份证',
        icon: 'none'
      })
    }else{
      // 提交
      wx.request({
        url: app.serverUrl + 'addBank',
        method: 'post',
        data: {
          openid: app.openid,
          username: self.data.username,
          password: self.data.password,
          idnumber: self.data.idnumber,
          idnumberup: self.data.idnumberup,
          idnumberdow: self.data.idnumberdow,
          idnumberuser: self.data.idnumberuser,
        },
        success: function (res) {
          console.log(res);
         if (res.data.flag == 'success') {
            wx.showToast({
              title: '已提交，等待审核',
              success: function () {
                wx.navigateTo({
                  url: '/pages/purse/purse',
                })
              }
            })
          } else {
            wx.showToast({
              icon: 'none',
              title: '提交失败',
            })
          }
        }
      })
    }
    // console.log(this.data);
  },
  getUsername:function(e){
    this.setData({
      username: e.detail.value
    })
  },
  getIdnumber: function (e) {
    this.setData({
      idnumber: e.detail.value
    })
  },
  getPassword: function (e) {
    this.setData({
      password: e.detail.value
    })
  },
  getRepassword: function (e) {
    this.setData({
      repassword: e.detail.value
    })
  },
  getIdnumberup: function (e) {
    this.setData({
      idnumberup: e.detail.value
    })
  },
  getIdnumberdow: function (e) {
    this.setData({
      idnumberdow: e.detail.value
    })
  },
  getIdnumberuser: function (e) {
    this.setData({
      idnumberuser: e.detail.value
    })
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '大易益·传统文化大舞台，爱好者的掌中宝！',
      path: '/pages/index/index'
    }
  }
  
})